package demo02;
//N字形变换(画图找规律)
class Solution {
    public String convert(String s, int numRows) {
        int d = 2 * numRows -2;//公差
        StringBuilder ret = new StringBuilder();
        if(numRows == 1) return s;//就只有一行
        //第一行
        for(int i = 0;i < s.length();i += d){
            ret.append(s.charAt(i));
        }
        //中间行
        for(int k = 1;k < numRows-1;k++){
            for(int i = k,j = d - k;i < s.length() || j < s.length();i+=d,j+=d){
                if(i < s.length()) ret.append(s.charAt(i));
                if(j < s.length()) ret.append(s.charAt(j));
            }
        }
        //最后一行
        for(int i = numRows-1;i < s.length();i+=d){
            ret.append(s.charAt(i));
        }
        return ret.toString();
    }
}